package com.yzy.mall.utils


import android.content.res.AssetManager
import com.google.gson.Gson
import com.yzy.baselibrary.app.BaseApplication
import com.yzy.mall.repository.ProvinceBean
import org.json.JSONArray
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.*


class AssetsUtils {
    private object SingletonHolder {
        val holder = AssetsUtils()
    }

    companion object {
        val instance = SingletonHolder.holder
    }

    private var options1Items: List<ProvinceBean> = ArrayList<ProvinceBean>()
    private val options2Items = ArrayList<ArrayList<String>>()
    private val options3Items = ArrayList<ArrayList<ArrayList<String>>>()

    init {
        val str = getJson()
        if (str != "") {
            val jsonBean: ArrayList<ProvinceBean> = parseData(str) //用Gson 转成实体
            /**
             * 添加省份数据
             *
             * 注意：如果是添加的JavaBean实体，则实体类需要实现 IPickerViewData 接口，
             * PickerView会通过getPickerViewText方法获取字符串显示出来。
             */
            options1Items = jsonBean
            jsonBean.forEach { bean ->
                val cityList = ArrayList<String>() //该省的城市列表（第二级）
                val province_AreaList = ArrayList<ArrayList<String>>() //该省的所有地区列表（第三极）
                   bean.city.forEach { city ->
                        val cityName: String = city.name
                        cityList.add(cityName) //添加城市
                        val city_AreaList = ArrayList<String>() //该城市的所有地区列表
                        city_AreaList.addAll(city.area)
                        province_AreaList.add(city_AreaList) //添加该省所有地区数据

                }
                /**
                 * 添加城市数据
                 */
                options2Items.add(cityList)
                /**
                 * 添加地区数据
                 */
                options3Items.add(province_AreaList)
            }
        }
    }


    private fun parseData(result: String?): ArrayList<ProvinceBean> { //Gson 解析
        val detail: ArrayList<ProvinceBean> = ArrayList<ProvinceBean>()
        try {
            val data = JSONArray(result)
            for (i in 0 until data.length()) {
                val entity: ProvinceBean = Gson().fromJson(data.optJSONObject(i).toString(), ProvinceBean::class.java)
                detail.add(entity)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return detail
    }
    fun getProvinceBean(): List<ProvinceBean> {
        return options1Items
    }

    fun getCityBean():ArrayList<ArrayList<String>> {
        return options2Items
    }
    fun getAreaBean():ArrayList<ArrayList<ArrayList<String>>>{
        return options3Items
    }

    private fun getJson(): String {
        val stringBuilder = StringBuilder()
        try {
            val assets: AssetManager = BaseApplication.getApp().assets
            val bf = BufferedReader(
                InputStreamReader(assets.open("province.json"))
            )
            var line: String?
            while (bf.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
            return stringBuilder.toString()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return ""
    }
}
